<!DOCTYPE HTML>
<html>
<!--
Test that calling setVisible('false') and then sending a low-memory
notification causes a WebGL context loss event.
-->
<head>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="../browserElementTestHelpers.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="application/javascript;version=1.7">
"use strict";

SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();
browserElementTestHelpers.enableProcessPriorityManager();

function runTest() {
  var iframe = document.createElement('iframe');
  iframe.setAttribute('mozbrowser', true);
  iframe.src = 'file_WebGLContextLost.html';

  // We use this to ensure that we don't call SimpleTest.finish() twice.
  var finished = false;
  function finishOnce() {
    if (!finished) {
      SimpleTest.finish();
      finished = true;
    }
  }

  expectMozbrowserEvent(iframe, 'error').then(function(e) {
    if (finished) {
      // We don't care if the frame dies after the test finishes.
      return;
    }
    todo(false, "child process is crashing; this probably indicates that " +
         "something is wrong with WebGL in child processes on your machine.");
    is(e.detail.type, 'fatal');
  }).then(finishOnce);

  var childID = null;
  Promise.all([
    expectOnlyOneProcessCreated('FOREGROUND').then(function(chid) {
      childID = chid;
    }),
    expectMozbrowserEvent(iframe, 'loadend'),
    expectMozbrowserEvent(iframe, 'showmodalprompt').then(function(e) {
      is(e.detail.message, 'ready');
    })
  ]).then(function() {
    // Fire a low-memory notification once the process goes into the background
    // due to the setVisible(false) call below.
    expectPriorityChange(childID, 'BACKGROUND').then(function() {
      SimpleTest.executeSoon(function() {
        var os = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
                              .getService(SpecialPowers.Ci.nsIObserverService);
        os.notifyObservers(null, "memory-pressure", "low-memory");
        ok(true, 'Successfully notified observers.');
      });
    });

    // This test isn't the only possible source of a low-memory notification; the
    // browser can fire one whenever it likes.  So it's fine if we lose the
    // WebGL context before we fire the low-memory notification ourself.

    var p = expectMozbrowserEvent(iframe, 'showmodalprompt').then(function(e) {
      is(e.detail.message, 'webglcontextlost');
    });

    iframe.setVisible(false);
    return p;
  }).then(finishOnce);

  document.body.appendChild(iframe);
}

addEventListener('testready', function() {
  // At the time this test was written, webgl was blocklisted inside child
  // processes on desktop Linux.  The issue is that we spawn a child process to
  // read driver info, but we only did this on the main prrocess.  Child
  // processes never read the driver info themselves, nor do they get it from
  // their parent, so they refuse to start up WebGL.
  //
  // This isn't a problem on B2G because we force WebGL on there.  But it
  // obviously makes this test difficult.  bjacob says forcing WebGL on here
  // shouldn't hurt things, and anyway this setting mirrors what we do on B2G,
  // which is what we're trying to test!
  SpecialPowers.pushPrefEnv({set: [["webgl.force-enabled", true]]},
                            runTest);
});

</script>
</body>
</html>
